﻿Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
<Serializable()> Public Class ResultGroups
    Private m_ResultAerageingTime(-1) As ResultAerageingTime
    Private m_PeriodNameAndData As New NameAndData '用于储存长期浓度的文件名及内容
    Private m_PeriodMaxTalbeResult As New MaxTalbeResult '用于储存期间平均前n最大值的计算结果
    ''废弃的对象-----------------------------
    Private m_PostResult As New PostResult
    '废弃的对象结束-----------------------------


    Private m_PostAerageingTime(-1) As PostResultAerageTime

    ''' <summary>
    ''' 用于保存数据的文件名
    ''' </summary>
    ''' <remarks></remarks>
    Private m_FileName As String

    Property PostAerageingTime() As PostResultAerageTime()
        Get
            Return Me.m_PostAerageingTime
        End Get
        Set(ByVal value As PostResultAerageTime())
            Me.m_PostAerageingTime = value
        End Set
    End Property
    ''' <summary>
    ''' 计算结果类
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ResultAerageingTime() As ResultAerageingTime()
        Get
            Return Me.m_ResultAerageingTime
        End Get
        Set(ByVal value As ResultAerageingTime())
            Me.m_ResultAerageingTime = value
        End Set
    End Property
    ''' <summary>
    ''' 用于储存期间平均所有点前n最大值的计算结果
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property PeriodMaxTalbeResult() As MaxTalbeResult
        Get
            Return m_PeriodMaxTalbeResult
        End Get
        Set(ByVal value As MaxTalbeResult)
            m_PeriodMaxTalbeResult = value
        End Set
    End Property


    ''' <summary>
    ''' 用于储存期间平均的计算结果的文件名称和内容的字符串
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property PeriodNameAndData() As NameAndData
        Get
            Return Me.m_PeriodNameAndData
        End Get
        Set(ByVal value As NameAndData)
            Me.m_PeriodNameAndData = value
        End Set
    End Property
    
    Property PostResult() As PostResult
        Get
            Return m_PostResult
        End Get
        Set(ByVal value As PostResult)
            m_PostResult = value
        End Set
    End Property
    ''' <summary>
    ''' 用于保存数据的文件名
    ''' </summary>
    ''' <remarks></remarks>
    Property FileName() As String
        Get
            Return Me.m_FileName
        End Get
        Set(ByVal value As String)
            Me.m_FileName = value
        End Set
    End Property
    Public Function GetData(ByVal strAERMODOUT As String(), ByVal Receptor As Receptor, ByVal IsDep As Boolean, ByVal AerageTime As AerageingTime, ByVal OutPutOption As OutPuts, ByVal strGroup As String, ByVal IsAnalisis As Boolean, ByVal GroupSourceCount As Integer, ByVal version As String, ByVal Control As Control, ByVal PostPath As String, ByVal Groups As Groups, ByVal iGroup As Integer)
        If m_PeriodNameAndData.GetPeriodDate(strAERMODOUT, Receptor, IsDep, strGroup, GroupSourceCount, version) = False Then
            Return False
        End If
        If AerageTime.Period = True Then
            GetPeriodMaxTalbeResult(strAERMODOUT, Receptor, IsDep, AerageTime, OutPutOption, strGroup, IsAnalisis)
        End If

        If AerageTime.H1 = True Then

            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime
            '计算各点高值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST  1-HR", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, " 1-HR", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If


            '计算Post文件相关的内容
            Dim strFileName As String = PostPath & Control.AllPolluts.AllSelectPollut(0).Name & "_" & Groups.ArrayGroup(iGroup).Name & "_POST_1h.BIN"
            ReDim Preserve Me.m_PostAerageingTime(Me.m_PostAerageingTime.Length)
            Me.m_PostAerageingTime(Me.m_PostAerageingTime.Length - 1) = New PostResultAerageTime
            If Me.m_PostAerageingTime(Me.m_PostAerageingTime.Length - 1).GetPostData(Control, Receptor, IsDep, m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.MaxCon(0).dateD, strFileName, version) = False Then
                Return False
            End If
        End If
        If AerageTime.H2 = True Then
            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST  2-HR", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, " 2-HR", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If
        End If
        If AerageTime.H3 = True Then
            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime

            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST  3-HR", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, " 3-HR", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If
        End If
        If AerageTime.H4 = True Then
            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime

            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST  4-HR", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, " 4-HR", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If
        End If
        If AerageTime.H6 = True Then
            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST  6-HR", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, " 6-HR", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If
        End If
        If AerageTime.H8 = True Then
            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST  8-HR", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, " 8-HR", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If
        End If
        If AerageTime.H12 = True Then
            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST 12-HR", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, "12-HR", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If
        End If
        If AerageTime.H24 = True Then
            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST 24-HR", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, "24-HR", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If
        End If
        If AerageTime.Month = True Then
            ReDim Preserve m_ResultAerageingTime(m_ResultAerageingTime.Length)
            m_ResultAerageingTime(m_ResultAerageingTime.Length - 1) = New ResultAerageingTime
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).GetDataFromAERMODOUT(strAERMODOUT, Receptor, IsDep, "HIGHEST MONTH", OutPutOption, strGroup, IsAnalisis, GroupSourceCount, version) = False Then
                Return False
            End If
            '计算区域前n最大值
            If m_ResultAerageingTime(m_ResultAerageingTime.Length - 1).MaxTalbeResult.GetMaxTalbeDateFromAERMODOUT(strAERMODOUT, OutPutOption, IsDep, "MONTH", strGroup, GroupSourceCount, version) = False Then
                Return False
            End If
        End If

        Return True
    End Function


    Public Function GetPeriodMaxTalbeResult(ByVal strAERMODOUT As String(), ByVal Receptor As Receptor, ByVal IsDep As Boolean, ByVal AerageTime As AerageingTime, ByVal OutPutOption As OutPuts, ByVal strGroup As String, ByVal IsAnalisis As Boolean) As Boolean
        '由于期间平均不能输出所有点前n大值，所以需要把所有点的期间平均均进行排序，然后输出前n个值。
        Try
            Dim MaxTalbeResult As New CAermod.MaxTalbeResult
            For i As Integer = 0 To Receptor.SubGridCart(0).Ynum - 1
                For j As Integer = 0 To Receptor.SubGridCart(0).Xnum - 1
                    ReDim Preserve MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length)
                    MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1) = New ResultCon
                    MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).con = Me.m_PeriodNameAndData.GridCon(i, j).con
                    MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).x = Receptor.SubGridCart(0).Xinit + Receptor.SubGridCart(0).Xdelta * j
                    MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).y = Receptor.SubGridCart(0).Yinit + Receptor.SubGridCart(0).Ydelta * (Receptor.SubGridCart(0).Ynum - 1 - i)
                Next
            Next
            '添加嵌套网格点的数据
            For k As Integer = 0 To Receptor.SubGridCart.Length - 1
                If Receptor.SubGridCart(k).Selected = True Then
                    For i As Integer = 0 To Receptor.SubGridCart(k).Ynum - 1
                        For j As Integer = 0 To Receptor.SubGridCart(k).Xnum - 1
                            ReDim Preserve MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length)
                            MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1) = New ResultCon
                            MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).con = Me.m_PeriodNameAndData.SubGridCon(k)(i, j).con
                            MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).x = Receptor.SubGridCart(k).Xinit + Receptor.SubGridCart(k).Xdelta * j
                            MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).y = Receptor.SubGridCart(k).Yinit + Receptor.SubGridCart(k).Ydelta * (Receptor.SubGridCart(k).Ynum - 1 - i)
                        Next
                    Next
                End If
            Next
            For i As Integer = 0 To Receptor.DiscCarts.Length - 1
                ReDim Preserve MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length)
                MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1) = New CAermod.ResultCon
                MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).con = Me.m_PeriodNameAndData.DiscCon(i).con
                MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).x = Me.m_PeriodNameAndData.DiscCon(i).x
                MaxTalbeResult.MaxCon(MaxTalbeResult.MaxCon.Length - 1).y = Me.m_PeriodNameAndData.DiscCon(i).y
            Next
            '对所有点的浓度进行排序
            If MaxTalbeResult.MaxCon.Length >= 2 Then
                For i As Integer = MaxTalbeResult.MaxCon.Length - 1 To 1 Step -1
                    For j As Integer = MaxTalbeResult.MaxCon.Length - 1 To MaxTalbeResult.MaxCon.Length - 1 - i + 1 Step -1
                        If MaxTalbeResult.MaxCon(j).con > MaxTalbeResult.MaxCon(j - 1).con Then
                            Dim ResultCon As New CAermod.ResultCon
                            ResultCon.con = MaxTalbeResult.MaxCon(j).con
                            ResultCon.dateD = MaxTalbeResult.MaxCon(j).dateD
                            ResultCon.x = MaxTalbeResult.MaxCon(j).x
                            ResultCon.y = MaxTalbeResult.MaxCon(j).y

                            MaxTalbeResult.MaxCon(j).con = MaxTalbeResult.MaxCon(j - 1).con
                            MaxTalbeResult.MaxCon(j).dateD = MaxTalbeResult.MaxCon(j - 1).dateD
                            MaxTalbeResult.MaxCon(j).x = MaxTalbeResult.MaxCon(j - 1).x
                            MaxTalbeResult.MaxCon(j).y = MaxTalbeResult.MaxCon(j - 1).y

                            MaxTalbeResult.MaxCon(j - 1).con = ResultCon.con
                            MaxTalbeResult.MaxCon(j - 1).dateD = ResultCon.dateD
                            MaxTalbeResult.MaxCon(j - 1).x = ResultCon.x
                            MaxTalbeResult.MaxCon(j - 1).y = ResultCon.y
                        End If
                    Next
                    If MaxTalbeResult.MaxCon.Length - 1 - i >= OutPutOption.MaxTable.nMax - 1 Then
                        ReDim Preserve MaxTalbeResult.MaxCon(OutPutOption.MaxTable.nMax - 1)
                        Exit For
                    End If
                Next
            End If
            Me.m_PeriodMaxTalbeResult = MaxTalbeResult
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function


    ''' <summary>
    ''' 清空所有的数据
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub UnLoadData()
        m_ResultAerageingTime = Nothing
        m_PeriodNameAndData = Nothing
        m_PeriodMaxTalbeResult = Nothing
        m_PostAerageingTime = Nothing

    End Sub

    Public Sub SaveData(ByVal Path As String, ByVal Name As String)
        Try
            ' Create a filestream object
            Dim fileStr As Stream = File.Open(Path & Name, FileMode.Create)
            ' Create a linked list object and populate it with random nodes
            Dim sdis As New ResultGroups
            sdis = Me
            ' Create a formatter object based on command line arguments
            Dim formatter As IFormatter
            formatter = CType(New BinaryFormatter, IFormatter)
            ' Serialize the object graph to stream
            formatter.Serialize(fileStr, sdis)
            ' All done
            fileStr.Close()
            UnLoadData()
            Me.m_FileName = Name
        Catch ex As Exception
            MsgBox("写dis文件错误!")
        End Try

    End Sub
    Public Function GetData(ByVal Path As String) As ResultGroups
        Try
            Dim fileStr As Stream = File.Open(Path & Me.m_FileName, FileMode.Open)

            ' Create a formatter object based on command line arguments
            Dim formatter As IFormatter
            formatter = CType(New BinaryFormatter, IFormatter)
            ' Deserialize the object graph from stream
            Dim sdis As New ResultGroups
            sdis = CType(formatter.Deserialize(fileStr), ResultGroups)
            fileStr.Close()
            Return sdis
        Catch ex As Exception
            MsgBox("读dis文件错误!")
            Return Nothing
        End Try
    End Function

    <OnDeserializing()> _
    Friend Sub OnDeserializingMethod(ByVal context As StreamingContext)
        m_PostResult = Nothing
    End Sub

    <OnDeserialized()> _
    Friend Sub OnDeserializedMethod(ByVal context As StreamingContext)
        '1.3.0.3版升级到1.3.0.4
        If Me.m_PostResult Is Nothing Then
            Me.m_PostResult = New PostResult
        End If
    End Sub
End Class
